package com.yiwei.hdb.adapter;

import android.content.Context;
import android.view.View;
import android.widget.RadioButton;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.base.BaseRViewSubAdapter;
import com.yiwei.hdb.adapter.base.BaseViewHolder;
import com.yiwei.hdb.constant.Const;
import com.yiwei.hdb.http.VolleySingleton;
import com.yiwei.hdb.interfaces.OnRecyclerViewListener;
import com.yiwei.hdb.model.GoPayModel;

import java.util.List;


/**
 * 支付方式
 * Created by MasterDong on 16/4/12.
 */

public class PayTypeAdapterNew extends BaseRViewSubAdapter<GoPayModel.PayType> {

    private int[] icon = {R.mipmap.yijipay_icon, R.mipmap.yijipay_icon, R.mipmap.alipay_icon};
    private String type = Const.PayType.YIJIPAY;
    private ImageLoader mImageLoader;

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private Context mContext;
    private List<GoPayModel.PayType> list;

    public PayTypeAdapterNew(Context context, List<GoPayModel.PayType> list) {
        super(context,R.layout.pay_type_items,list);
        this.list = list;
        this.mContext = context;
        mImageLoader = VolleySingleton.getImageLoader();
    }



    @Override
    protected void convert(BaseViewHolder viewHoder, GoPayModel.PayType item, final int  position) {
        GoPayModel.PayType mode = item;
        viewHoder.getTextView(R.id.title).setText(mode.getPayment_name());
        viewHoder.getTextView(R.id.describe).setText(mode.getPayment_description());
        NetworkImageView iconView=(NetworkImageView) viewHoder.getView(R.id.icon);
        iconView.setImageUrl(mode.getPayment_logo(), mImageLoader);

        if ((mode.getPayment_class_name().toLowerCase()).equals(Const.PayType.YIJIPAY)) {
            iconView.setDefaultImageResId(icon[0]);
            iconView.setErrorImageResId(icon[0]);
        }
        if ((mode.getPayment_class_name().toLowerCase()).equals(Const.PayType.YIJIPAY_WECHAT)) {
            iconView.setDefaultImageResId(icon[1]);
            iconView.setErrorImageResId(icon[1]);
        }
        if ((mode.getPayment_class_name().toLowerCase()).equals(Const.PayType.ALIPAY)) {
            iconView.setDefaultImageResId(icon[2]);
            iconView.setErrorImageResId(icon[2]);
        }
        if ((mode.getPayment_class_name().toLowerCase()).equals(Const.PayType.Otherpay)) {
            iconView.setDefaultImageResId(icon[0]);
            iconView.setErrorImageResId(icon[0]);
        }

        if (type.equals(mode.getPayment_class_name().toLowerCase())) ((RadioButton)viewHoder.getView(R.id.rb)).setChecked(true);
        else ((RadioButton)viewHoder.getView(R.id.rb)).setChecked(false);

        View rootView = viewHoder.getView(R.id.view);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onRecyclerViewListener) {
                    type = list.get(position).getPayment_class_name().toLowerCase();

                    onRecyclerViewListener.onItemClick(list, position);
                }
            }
        });
        rootView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (null != onRecyclerViewListener) {
                    return onRecyclerViewListener.onItemLongClick(list, position);
                }
                return false;
            }
        });

    }


}

